package com.gqz.patrol.app.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.gqz.common.core.domain.RequestForm;
import com.gqz.common.core.utils.query.QueryMapperUtil;
import com.gqz.patrol.app.entity.EnPatrolTaskOperateEntity;
import com.gqz.patrol.app.mapper.EnPatrolTaskOperateMapper;
import com.gqz.patrol.app.mapstruct.EnPatrolTaskMapstruct;
import com.gqz.patrol.app.mapstruct.EnPatrolTaskOperateMapstruct;
import com.gqz.patrol.app.service.IEnPatrolTaskOperateService;
import com.gqz.patrol.vo.EnPatrolTaskOperateVo;
import com.gqz.patrol.vo.EnPatrolTaskVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class EnPatrolTaskOperateServiceImpl extends ServiceImpl<EnPatrolTaskOperateMapper, EnPatrolTaskOperateEntity> implements IEnPatrolTaskOperateService {

    @Resource
    private EnPatrolTaskOperateMapstruct mapstruct;

    @Resource
    private EnPatrolTaskMapstruct taskMapstruct;

    @Override
    public List<EnPatrolTaskOperateVo> getById(String id) {
        MPJLambdaWrapper<EnPatrolTaskOperateEntity> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.eq(EnPatrolTaskOperateEntity::getId, id);
        queryWrapper.selectAll(EnPatrolTaskOperateEntity.class);
        List<EnPatrolTaskOperateEntity> entities = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(entities)) {
            return mapstruct.toVos(entities);
        }
        return new ArrayList<>();
    }

    @Override
    public Page<EnPatrolTaskOperateVo> page(RequestForm<EnPatrolTaskOperateVo> requestForm) {
        EnPatrolTaskOperateEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnPatrolTaskOperateEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.selectAll(EnPatrolTaskOperateEntity.class);
        Page<EnPatrolTaskOperateEntity> page = Page.of(requestForm.getPageNo(), requestForm.getPageSize());
        //page = baseMapper.selectPage(page, queryWrapper);
        IPage<EnPatrolTaskOperateEntity> iPage = baseMapper.selectJoinPage(page, EnPatrolTaskOperateEntity.class, queryWrapper);
        page.setRecords(iPage.getRecords());
        page.setTotal(iPage.getTotal());
        return mapstruct.toVos(page);
    }

    @Override
    public Long count(RequestForm<EnPatrolTaskOperateVo> requestForm) {
        EnPatrolTaskOperateEntity entity = mapstruct.toEntity(requestForm.getEntity());
        MPJLambdaWrapper<EnPatrolTaskOperateEntity> queryWrapper = QueryMapperUtil.initQueryWrapper(entity, requestForm);
        queryWrapper.select(EnPatrolTaskOperateEntity::getId);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int insert(List<EnPatrolTaskOperateVo> vos) {
        List<EnPatrolTaskOperateEntity> entitys = mapstruct.toEntitys(vos);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnPatrolTaskOperateEntity entity = entitys.get(i);
            result += baseMapper.insert(entity);
        }
        return result;
    }

    @Override
    public int update(List<EnPatrolTaskOperateVo> forms) {
        List<EnPatrolTaskOperateEntity> entitys = mapstruct.toEntitys(forms);
        int result = 0;
        for (int i = 0, size = entitys.size(); i < size; i++) {
            EnPatrolTaskOperateEntity entity = entitys.get(i);
            result += baseMapper.updateById(entity);
        }
        return result;
    }


    /**
     * 保存工单的记录
     *
     * @param taskVo
     * @param taskNos
     * @return
     */
    @Override
    public int saveOperation(EnPatrolTaskVo taskVo, List<String> taskNos) {
        taskNos.forEach(taskNo -> {
            EnPatrolTaskOperateEntity operateEntity = new EnPatrolTaskOperateEntity();
            operateEntity.setTaskNo(taskNo);
            operateEntity.setOptTime(taskVo.getCreatTime());//DateUtil.parse(taskVo.getCreatTime(),"yyyy-MM-dd HH:mm:ss")
            operateEntity.setOptPersonId("~");
            operateEntity.setOptPersonName(taskVo.getCreator());
            operateEntity.setOptStatus("0");
            operateEntity.setOptStatusName("工单编制");

            boolean b = save(operateEntity);
        });
        return 0;
    }

}
